package com.hui.auth.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hui.auth.dto.JsonResult;
import com.hui.auth.entity.AuthRole;
import com.hui.auth.exception.AuthError;
import com.hui.auth.service.AuthRoleService;
import com.hui.utils.ObjAnalysisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色控制
 * Created by LiaoYunHui on 2017/11/5/005.
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping(value = "auth/role")
public class RoleController {

    @Autowired
    private AuthRoleService roleService;

    /**
     * 新增用户
     *
     * @param role
     * @return
     */
    @ApiOperation(value = "新增角色")
    @PostMapping(value = "insert")
    public JsonResult insert(@RequestBody @Validated AuthRole role, BindingResult result) throws Exception {
        JsonResult jsonResult = new JsonResult();
        if (result.hasErrors()) {
            return jsonResult.setMsg(result.getFieldErrors().get(0).getDefaultMessage()).setCode(AuthError.OPERATION_FAILED.code);
        }
        roleService.insert(role);
        return jsonResult;
    }

    /**
     * 根据Id查询角色信息
     *
     * @return
     */
    @ApiOperation(value = "根据Id查询角色信息")
    @GetMapping(value = "select")
    public JsonResult<Map<String, Object>> select(@RequestParam Long rid) throws Exception {
        JsonResult<Map<String, Object>> jsonResult = new JsonResult<Map<String, Object>>();
        Map<String, Object> role = roleService.select(rid);
        jsonResult.setData(role);
        return jsonResult;
    }

    /**
     * 查询所有
     *
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping(value = "select_all")
    public JsonResult<Map<String, Object>> selectAll(@RequestParam Integer pageNo, @RequestParam Integer pageSize, @RequestParam String search) {
        JsonResult<Map<String, Object>> jsonResult = new JsonResult<>();
        Map<String, Object> params = new HashMap<>();
        params.put("search", search);
        Map<String, Object> data = new HashMap<>();
        Page p = PageHelper.startPage(pageNo, pageSize);
        List<AuthRole> list = roleService.selectAll(params);
        data.put("list", list);
        data.put("count", p.getTotal());
        jsonResult.setData(data);
        return jsonResult;
    }

    /**
     * 修改
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "修改")
    @PostMapping(value = "update")
    public JsonResult update(@RequestBody Map requestBody) throws Exception {
        JsonResult jsonResult = new JsonResult();
        AuthRole role = (AuthRole) ObjAnalysisUtil.convertMap(AuthRole.class, (Map) requestBody.get("role"));
        List<Integer> menus = (List<Integer>) requestBody.get("menu");
        List<Integer> perms = (List<Integer>) requestBody.get("permission");
        if (roleService.update(role, menus, perms)) {
            jsonResult.setMsg("修改成功");
            return jsonResult;
        }
        jsonResult.setCode(AuthError.OPERATION_FAILED.code);
        jsonResult.setMsg(AuthError.OPERATION_FAILED.msg);
        return jsonResult;
    }

    /**
     * 删除（真删除）
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @PostMapping(value = "delete/{id}")
    public JsonResult<Boolean> deleteUserById(@PathVariable Integer id) {
        JsonResult<Boolean> jsonResult = new JsonResult<Boolean>();
        if (roleService.delete(id)) {
            jsonResult.setData(true);
            return jsonResult;
        }
        jsonResult.setCode(AuthError.OPERATION_FAILED.code);
        jsonResult.setMsg(AuthError.OPERATION_FAILED.msg);
        return jsonResult;
    }
}
